﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WLive.WLiveObjects
{
    public class Folder:Operational<Folder>
    {
        public From from;
        public int count;
        public string link;
        public string upload_location;
        public bool is_embeddable;
        public string type;
        public DateTime created_time;
        public DateTime updated_time;
        public DateTime client_updated_time;
        public SharedWith shared_with;
        [Writable]
        public string sort_by;
        /// <summary>
        /// 
        /// </summary>
        /// <returns>Returns list of folders of current folder</returns>
        public IEnumerable<Folder> GetFolders()
        {
            try
            {
                return GetFolders(System.IO.SearchOption.TopDirectoryOnly);
            }
            catch (Exception e)
            { throw e; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="option">Option for search in current folder or is subfolders</param>
        /// <returns>Returns list of folders or subfolders of current fulder</returns>
        public IEnumerable<Folder> GetFolders(System.IO.SearchOption option)
        {
            try
            {
                return GetFolders(id, option);
            }
            catch (Exception e)
            { throw e; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="folderid">FOLDER_ID</param>
        /// <returns>Returns list of folders of folder based od location id</returns>
        public static IEnumerable<Folder> GetFolders(string folderid)
        {
            try
            {
                return GetFolders(folderid, System.IO.SearchOption.TopDirectoryOnly);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="folderid">FOLDER_ID</param>
        /// <param name="option">Option for search in current folder or is subfolders</param>
        /// <returns>Returns list of folders or subfolders of folder based od location id</returns>
        public static IEnumerable<Folder> GetFolders(string folderid, System.IO.SearchOption option)
        {
            if (string.IsNullOrEmpty(folderid))
                folderid = "me/skydrive";
            Filter f = new Filter(FilterTypes.FOLDER | FilterTypes.ALBUM);
            QueryParameters qp = new QueryParameters();
            qp.Add("filter", f.GetTypesForQuery());
            try
            {
                if (option == System.IO.SearchOption.TopDirectoryOnly)
                    return Requester.Request<Folder>(new RequestObject { url = UrlBuilder1.Build(folderid + "/files", qp) }).ToList();
                else
                {
                    List<Folder> ret = new List<Folder>();
                    foreach (Folder folder in Requester.Request<Folder>(new RequestObject { url = UrlBuilder1.Build(folderid + "/files", qp) }))
                    {
                        ret.Add(folder);
                        ret.AddRange(GetFolders(folder.id, option));
                    }
                    return ret;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns>Returns files in current folder</returns>
        public IEnumerable<File> GetFiles()
        {
            try
            {
                return GetFiles(id);
            }
            catch (Exception e)
            { throw e; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="K">Type of file which you want to return. If you want to return more types, use type File</typeparam>
        /// <param name="filter">Types of file which you want to return.</param>
        /// <returns>Return files of current folder</returns>
        public IEnumerable<K> GetFiles<K>(FilterTypes filter)
        {
            try
            {
                return GetFiles<K>(id, filter);
            }
            catch (Exception e)
            { throw e; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="folderid">FOLDER_ID</param>
        /// <returns>Return files in folder get by folderid</returns>
        public static IEnumerable<File> GetFiles(string folderid)
        {
            try
            {
                if (string.IsNullOrEmpty(folderid))
                    folderid = "me/skydrive";
                return Requester.Request<File>(new RequestObject { url = UrlBuilder1.Build(folderid + "/files") }).ToList();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="K">Type of file which you want to return. If you want to return more types, use type File</typeparam>
        /// <param name="folderid">FOLDER_ID</param>
        /// <param name="filter">Types of file which you want to return.</param>
        /// <returns>Return files in folder get by folderid</returns>
        public static IEnumerable<K> GetFiles<K>(string folderid, FilterTypes filter)
        {
            QueryParameters qp = new QueryParameters();
            Filter f = new Filter(filter);
            qp.Add("filter", f.GetTypesForQuery());
            try
            {
                if (string.IsNullOrEmpty(folderid))
                    folderid = "me/skydrive";
                return Requester.Request<K>(new RequestObject { url = UrlBuilder1.Build(folderid + "/files", qp) }).ToList();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
